var express= require('express')
var path=require('path')
var mongoose=require('mongoose')
var _=require('underscore')
var bodyParser = require('body-parser');
var Movie=require('./models/movie')
var port =process.env.PORT ||3000
var app=express()
// var ejs = require('ejs')

mongoose.connect('mongodb://mongo:Bmsoft123@139.159.212.111:12345/test')


app.set('views','./views/pages')
// app.set('view engine','ejs')
app.set('view engine','jade')

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))
// parse application/json
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname,'public')))
app.locals.moment=require('moment')
app.listen(port)

console.log('imooc started on port'+port)

//router
app.get('/',function(req,res){
    Movie.fetch(function(err,movieList){
        res.render('index',{
            title:'imooc 首页',
            movies:movieList
        })
    })


})
app.get('/movie/:id',function(req,res){
    var id=req.params.id
    Movie.findById(id,function(err,movie){
        if(err)
            console.log(err)
        else
            res.render('detail',{
                title:movie.title,
                movie:movie
            })
    })
})

//admin post movie
app.post('/admin/movie/new',function(req,res){
    var id = req.body.movie._id
    var movieObj=req.body.movie
    var _movie
    if(id!=='undefined'){
        Movie.findById(id,function(err,movie){
            if(err){
                console.log(err)
            }
            _movie=_.extend(movie,movieObj)
            _movie.save(function(err,movie){
                if(err){
                    console.log(err)
                }
                res.redirect('/movie/'+movie._id)
            })
        })
    }
    else{
        _movie=new Movie({
            doctor:movieObj.doctor,
            title:movieObj.title,
            country:movieObj.country,
            language:movieObj.language,
            year:movieObj.year,
            poster:movieObj.poster,
            summary:movieObj.summary,
            flash:movieObj.flash
        })
        _movie.save(function(err,movie){
            if(err){
                console.log(err)
            }
            res.redirect('/movie/'+movie._id)
        })
    }
})

app.get('/admin/movie',function(req,res){
    res.render('admin',{
        title:'后台录入页',
        movie:{
            title:'',
            doctor:'',
            country:'',
            year:'',
            poster:'',
            flash:'',
            summary:'',
            language:''
        }
    })
})
app.get('/admin/list',function(req,res){
    Movie.fetch(function(err,list){
        if(err)
            console.log(err)
        else
            res.render('list',{
                title:'管理员列表页',
                movies:list
            })
    })
})
app.get('/admin/update/:id',function(req,res){
    var id=req.params.id
    Movie.findById(id,function(err,movie){
        if(err)
            console.log(err)
        else
            res.render('admin',{
                title:movie.title,
                movie:movie
            })
    })
})
app.delete('/admin/list',function(req,res){
    var id=req.query.id

    if(id){
        Movie.remove({_id:id},function (err,movie) {
            if(err)
                console.log(err)
            else
                res.json({success:1})
        })
    }
})